import { defineStore } from 'pinia'
import api from '@/api'

export const teacherclass = defineStore('teacherclass', {
    state: () => ({
        classinfoList: [] as Array<{
            id: string;
            stage: number;
            classBaseinfoDTO: {
                classId: string;
                major: string;
                termNumber: number;
            };
            createdTime:string;
        }>
    }),
    actions: {
        async getTeacherClassById(userId: string) {
            console.log("获取教师班级信息, userId:", userId);
            // 调用API获取教师班级信息
            await api.teacherClass.getTeacherClassById(userId).then(res => {
                console.log("获取教师班级信息:", res);
                this.classinfoList = res.data as any;
            }).catch(error => {
                console.error("获取教师班级信息失败:", error);
                throw error;
            });
        },
    },
    getters: {
        // 返回组合后的班级信息数组
        getClassInfo(): Array<{
            id: string;
            className: string;
            stages: number[];
        }> {
            const classMap = new Map<string, {
                id: string;
                className: string;
                stages: number[];
                // createdTime: Date;
            }>();
            this.classinfoList.forEach(item => {
                const key = `${item.classBaseinfoDTO.major}第${item.classBaseinfoDTO.termNumber}期`;//班级的输出格式
                if (!classMap.has(key)) {
                    classMap.set(key, {
                        id: item.classBaseinfoDTO.classId,
                        className: key,
                        stages: []
                        // createdTime:
                    });
                }
                classMap.get(key)!.stages.push(item.stage);
            });
                return Array.from(classMap.values());
        }
    }
});
